package com.cmc.cloud.cmclink.doc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.lock.annotation.Lock4j;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.RouteMatchCompanyErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.vo.routematchcompanyvo.RouteMatchCompanyBaseVO;
import com.cmc.cloud.cmclink.doc.vo.routematchcompanyvo.RouteMatchCompanyCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.routematchcompanyvo.RouteMatchCompanyPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.routematchcompanyvo.RouteMatchCompanyUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;


import java.util.*;

import com.cmc.cloud.cmclink.doc.service.RouteMatchCompanyService;
import com.cmc.cloud.cmclink.doc.entity.RouteMatchCompanyDO;
import com.cmc.cloud.cmclink.doc.mapper.RouteMatchCompanyMapper;


/**
 * 路径的经营主体 Service 实现类
 *
 * @author 招商轮船-管理员
 */
@Service
@Validated
public class RouteMatchCompanyServiceImpl implements RouteMatchCompanyService {

    @Resource
    private RouteMatchCompanyMapper routeMatchCompanyMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Lock4j(name = "routeMatchUnique", keys = {"#createReqVO.getRouteId"})
    public void createRouteMatchCompany(RouteMatchCompanyCreateReqVO createReqVO) {
        // 插入
        validateUniqueExists(createReqVO, null);
        RouteMatchCompanyDO routeMatchCompany = BeanUtil.copyProperties(createReqVO, RouteMatchCompanyDO.class);
        routeMatchCompanyMapper.insert(routeMatchCompany);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Lock4j(name = "routeMatchUnique", keys = {"#updateReqVO.getRouteId"})
    public void updateRouteMatchCompany(RouteMatchCompanyUpdateReqVO updateReqVO) {
        // 校验存在
        validateRouteMatchCompanyExists(updateReqVO.getId());
        validateUniqueExists(updateReqVO, updateReqVO.getId());
        // 更新
        RouteMatchCompanyDO updateObj = BeanUtil.copyProperties(updateReqVO, RouteMatchCompanyDO.class);
        routeMatchCompanyMapper.updateById(updateObj);
    }

    @Override
    public void deleteRouteMatchCompany(Long id) {
        // 校验存在
        validateRouteMatchCompanyExists(id);
        // 删除
        routeMatchCompanyMapper.deleteById(id);
    }

    private void validateRouteMatchCompanyExists(Long id) {
        if (routeMatchCompanyMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(RouteMatchCompanyErrorCodeConstants.ROUTE_MATCH_COMPANY_NOT_EXISTS);
        }
    }


    private void validateUniqueExists(RouteMatchCompanyBaseVO vo, Long id) {
        LambdaQueryWrapperX<RouteMatchCompanyDO> queryWrapperX = new LambdaQueryWrapperX<RouteMatchCompanyDO>()
                .eqIfPresent(RouteMatchCompanyDO::getRouteId, vo.getRouteId());

        if (id != null) {
            queryWrapperX.ne(RouteMatchCompanyDO::getId, id);
        }
        if (routeMatchCompanyMapper.selectCount(queryWrapperX) != 0L) {
            throw ServiceExceptionUtil.exception(RouteMatchCompanyErrorCodeConstants.ROUTE_MATCH_COMPANY_EXISTED);
        }
    }


    @Override
    public RouteMatchCompanyDO getRouteMatchCompany(Long id) {
        return routeMatchCompanyMapper.selectById(id);
    }

    @Override
    public List<RouteMatchCompanyDO> getRouteMatchCompanyList(Collection<Long> ids) {
        return routeMatchCompanyMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<RouteMatchCompanyDO> getRouteMatchCompanyPage(RouteMatchCompanyPageReqVO reqVO) {
        LambdaQueryWrapperX<RouteMatchCompanyDO> queryWrapperX = new LambdaQueryWrapperX<RouteMatchCompanyDO>()
                .eqIfPresent(RouteMatchCompanyDO::getRouteId, reqVO.getRouteId())
                .likeIfPresent(RouteMatchCompanyDO::getCompany, reqVO.getCompany())
                .likeIfPresent(RouteMatchCompanyDO::getRouteDesc, reqVO.getRouteDesc());
        return routeMatchCompanyMapper.selectPage(reqVO, queryWrapperX);
    }
}
